package com.tucy.logistics.modules.system.role.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tucy.logistics.common.dto.system.QueryRoleDto;
import com.tucy.logistics.common.entity.user.SystemRole;
import com.tucy.logistics.modules.system.role.service.SystemRoleService;
import com.tucy.logistics.modules.system.role.mapper.SystemRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author tucy123
* @description 针对表【system_role(角色表)】的数据库操作Service实现
* @createDate 2023-09-14 15:31:23
*/
@Service
public class SystemRoleServiceImpl extends ServiceImpl<SystemRoleMapper, SystemRole> implements SystemRoleService{

    @Autowired
    SystemRoleMapper systemRoleMapper;

    /**
     * 查询角色
     * @param role
     * @return
     */
    @Override
    public Page<SystemRole> queryRole(QueryRoleDto role) {
        Page<SystemRole> rolePage = new Page<>(role.getPageNum(),role.getPageSize());
        QueryWrapper<SystemRole> wrapper = new QueryWrapper<>();

        wrapper.lambda()
                .eq(ObjectUtil.isNotEmpty(role.getId()),SystemRole::getId,role.getId())
                .like(ObjectUtil.isNotEmpty(role.getRoleName()),SystemRole::getRoleName,role.getRoleName())
                .or().like(ObjectUtil.isNotEmpty(role.getRoleCode()),SystemRole::getRoleCode,role.getRoleCode());

        return systemRoleMapper.selectPage(rolePage,wrapper);
    }

    /**
     * 增改角色
     * @param role
     * @return
     */
    @Override
    public Boolean saveUpdateRole(SystemRole role) {
        role.setSort(Math.toIntExact(this.count()) + 1);
        return this.saveOrUpdate(role);
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteRole(List<Long> ids) {
        int i = systemRoleMapper.deleteBatchIds(ids);
        return i == 1;
    }
}




